package com.github.sd4324530.fastweixin.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.sd4324530.fastweixin.api.config.WxConfig;
import com.github.sd4324530.fastweixin.api.entity.PoiForUpdateModel;
import com.github.sd4324530.fastweixin.api.entity.PoiModel;
import com.github.sd4324530.fastweixin.api.enums.ResultType;
import com.github.sd4324530.fastweixin.api.response.BaseResponse;
import com.github.sd4324530.fastweixin.api.response.GetPoiListResponse;
import com.github.sd4324530.fastweixin.util.BeanUtil;
import com.github.sd4324530.fastweixin.util.JSONUtil;

/**
 * 门店相关API
 *
 * @author eric
 * @since 1.2
 */
public class PoiAPI extends BaseAPI {

    private static final Logger LOG = LoggerFactory.getLogger(PoiAPI.class);

    public PoiAPI(WxConfig config) {
        super(config);
    }

    /**
     * 创建门店
     *
     * @param PoiModel 门店对象
     * @return 调用结果
     */
    public ResultType createPoi(PoiModel poi) {
        BeanUtil.requireNonNull(poi, "poi is null");
        String url = BASE_API_URL+"cgi-bin/poi/addpoi?access_token=#";
        BaseResponse response = executePost(url, "{\"business\":{\"base_info\":"+poi.toJsonString()+"}}");
        return ResultType.get(response.getErrcode());
    }

    public ResultType updatePoi(PoiForUpdateModel pum){
    	 BeanUtil.requireNonNull(pum, "pum is null");
    	  String url = BASE_API_URL+"cgi-bin/poi/updatepoi?access_token=#";
          BaseResponse response = executePost(url, "{\"business\":{\"base_info\":"+pum.toJsonString()+"}}");
          return ResultType.get(response.getErrcode());
    }
    
    public ResultType deletePoi(String poiId){
    	BeanUtil.requireNonNull(poiId, "poiId is null");
  	  	String url = BASE_API_URL+"cgi-bin/poi/delpoi?access_token=#";
        BaseResponse response = executePost(url,JSONUtil.getJSONFromString(poiId).toJSONString());
        return ResultType.get(response.getErrcode());
    }
    
    public GetPoiListResponse getPoiList(Integer begin,Integer limit){
    	GetPoiListResponse response = null;
    	String url = BASE_API_URL+"cgi-bin/poi/getpoilist?access_token=#";
    	BaseResponse r = executePost(url, "{\"begin\":"+begin+",\"limit\":"+limit+"}");
    	String resultJson = isSuccess(r.getErrcode()) ? r.getErrmsg() : r.toJsonString();
        response = JSONUtil.toBean(resultJson, GetPoiListResponse.class);
    	return response;
    }
   
}
